﻿using System;
using System.Text;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
/*
Class  : StringCrypt.cs
Aurthor: Departure
Url    : im-integrations.com
Info:
Encrypt Strings with Vigenere Cipher and ROT Cipher(Caesar Cipher, Rot13 ect..)
Based on information from wiki
*/

internal class StringCrypt
{

    public static string CalculateMD5Hash(string input)
    {
        // step 1, calculate MD5 hash from input
        MD5 md5 = MD5.Create();
        byte[] inputBytes = Encoding.ASCII.GetBytes(input);
        byte[] hash = md5.ComputeHash(inputBytes);

        // step 2, convert byte array to hex string
        StringBuilder sb = new StringBuilder();
        for (int i = hash.Length - 1; i >= 0; i--)
        {
            sb.Append(hash[i].ToString("X2"));
        }

        // step 3, remove numbers and return
        // Regex.Replace(sb.ToString(), @"\d", "")

        return sb.ToString();
    }


    public static string Vigenere(string sSource, string sKey, int iTableSize = 94, bool bDecrypt = false)
    {
        //Variables
        int i = 32;
        int iPosText;
        int iPosKey;
        string sTable = "";
        string sResult = "";

        //Create Table
        while (i < (iTableSize + 32))
        {
            sTable += ((char)i); i++;
        }
        //Make Key same size as Cipher
        while (sSource.Length >= sKey.Length)
        {
            sKey = string.Concat(sKey, sKey);
        }

        //Vigenere Routine
        i = 0;
        while (i <= (sSource.Length - 1))
        {
            if (sTable.IndexOf(sSource[i]) == -1)
                sResult += sSource[i];
            else
            {
                iPosText = sTable.IndexOf(sSource[i]);
                iPosKey = sTable.IndexOf(sKey[i]);
                if (bDecrypt)
                    sResult += sTable[(((iPosText + iTableSize) - iPosKey) % iTableSize)];
                else
                    sResult += sTable[((iPosText + iPosKey) % iTableSize)];
            }
            i++;

        }

        return sResult;

    }

    public static string Caesar(string sSource, int iKey, bool bDecrypt = false)
    {
        //Variables
        string sResult = "";
        string sTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        int iPosText;
        int i;

        //Convert to Uppercase
        sSource = sSource.ToUpper();

        //Caesar Routine
        i = 0;
        while (i <= (sSource.Length - 1))
        {
            if (sTable.IndexOf(sSource[i]) == -1)
                sResult += sSource[i];
            else
            {
                iPosText = sTable.IndexOf(sSource[i]);
                if (bDecrypt)
                    sResult += sTable[(((iPosText + sTable.Length) - iKey) % sTable.Length)];
                else

                    sResult += sTable[((iPosText + iKey) % sTable.Length)];
            }
            i++;
        }

        return sResult;

    }

    public static string ROT13(string sSource)
    {
        return Caesar(sSource, 13, false);
    }
}